import { requestModel, registerEntityType } from '../base'
import { registerEntityTable } from '../table'
import type { Waypoint } from './types'
import { initWaypointIndex } from './geo-index'
import { initMap } from './map'
import { initWaypointMapItem } from './map-item'
import { lat2str, lng2str } from '@/geom/great-circle'

export type { Waypoint } from './types'
export * from './feature'
export { waypointMapItem } from './map-item'

function setupWaypointTable() {
  registerEntityTable<Waypoint>({
    type: 'Waypoint',
    columns: [
      {
        name: 'id',
        cell: (e) => e.id,
        width: 100
      },
      {
        name: '经度',
        cell: (e) => lng2str(e.location.lng),
        width: 100
      },
      {
        name: '纬度',
        cell: (e) => lat2str(e.location.lat),
        width: 100
      }
    ]
  })
}

export async function setupWaypoint() {
  const data = (await requestModel('Waypoint')) as Waypoint[]
  registerEntityType<Waypoint>({
    type: 'Waypoint',
    typeName: '航路点',
    data
  })
  setupWaypointTable()
  initWaypointIndex(data)
  initMap(data)
  initWaypointMapItem()
}
